#!/usr/bin/env bash
set -x
SELFDIR=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)

# $1 install
# $2 <module_name>
# $3 <module_version>
function main()
{
    if [[ $# -lt 3 ]]; then
        return 1
    fi

    if [[ ! -d "${PKGREPO}" ]]; then
        echo    "The required environment variable \${PKGREPO} is missing"
        return  1
    fi

    if [[ ! -d "${DEVREPO}" ]]; then
        echo    "The required environment variable \${DEVREPO} is missing"
        return  1
    fi

    #   初始化
    source "${PKGREPO}/.share/module-install-init.bash" "$@"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "The required environment variable \${DEVREPO} is missing"
        return  1
    fi

    #   确定主包和主包内部目录
    local antlr4jar=$(cat "${MODULE_PACKAGE_DIR}/VERSION" | grep PACKAGE | grep ":jar:" | awk '{print $3}')
    local antlr4cpp=$(cat "${MODULE_PACKAGE_DIR}/VERSION" | grep PACKAGE | grep ":cpp:" | awk '{print $3}')

    #   备份旧软件、创建构建目录、解压主软件包
    framework backup      &&  \
    framework maketransit &&  \
    framework unzip "${MODULE_PACKAGE_DIR}/${antlr4cpp}" "${MODULE_TRANSIT_DIR}"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    ""
        return  1
    fi


    local builddir="${MODULE_TRANSIT_DIR}/build"
    mkdir -p "${builddir}"
    cd      "${builddir}"    &&  \
    cmake ..  \
        -DANTLR_JAR_LOCATION="${MODULE_PACKAGE_DIR}/${antlr4jar}" \
        -DANTLR_BUILD_CPP_TESTS=No  \
        -DCMAKE_BUILD_TYPE=Release  \
        -DCMAKE_INSTALL_PREFIX="${MODULE_INSTALL_DIR}"  &&  \
    make  -j ${MODULE_TRANSIT_THREAD}                   &&  \
    make  install   && \
    echo ""
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "Compile and install cpp-runtime package '${RESULT}' failed(${RESULT})"
        return  1
    fi

    #   安装
    mkdir -p    "${MODULE_INSTALL_DIR}"                                                     &&  \
    cp -rf      "${SELFDIR}/install"/*  "${MODULE_INSTALL_DIR}"                             &&  \
    cp -rf      "${MODULE_PACKAGE_DIR}/${antlr4jar}" "${MODULE_INSTALL_DIR}/lib"            &&  \
    cat         "${MODULE_INSTALL_DIR}/bin/antlr4-tmpl" \
      | sed -e  "s#{{ANTLR4_JAR}}#${antlr4jar}#g" > "${MODULE_INSTALL_DIR}/bin/antlr4"      &&  \
    chmod +x    "${MODULE_INSTALL_DIR}/bin/antlr4"                                          &&  \
    echo        "Build and install '${MODULE_LABEL}' success"
    RESULT=$?
    if [[ ${RESULT} -ne 0 ]]; then
        echo    "Build and install '${MODULE_LABEL}' failed(${RESULT}): '${MODULE_TRANSIT_DIR}'"
        return  1
    fi

    #   删除原始的模板
    rm -rf "${MODULE_INSTALL_DIR}/bin/antlr4-tmpl"

    #   写 setup 文件
    touch "${MODULE_INSTALL_DIR}/setup.bash"
    echo "export ${MODULE_NAME_UPPER}_HOME=\"\${DEVREPO}/${MODULE_LABEL}\""     >>   "${MODULE_INSTALL_DIR}/setup.bash"
    echo "export PATH=\"\${${MODULE_NAME_UPPER}_HOME}/bin:\${PATH}\""           >>   "${MODULE_INSTALL_DIR}/setup.bash"
    #
    touch "${MODULE_INSTALL_DIR}/setup.csh"
    echo "setenv ${MODULE_NAME_UPPER}_HOME \"\${DEVREPO}/${MODULE_LABEL}\""     >>   "${MODULE_INSTALL_DIR}/setup.csh"
    echo "setenv PATH \"\${${MODULE_NAME_UPPER}_HOME}_HOME:\${PATH}\""          >>   "${MODULE_INSTALL_DIR}/setup.csh"

    return 0
}

main    "$@"
exit    "$?"
